unit Test;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Lingr, Contnrs;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    Button2: TButton;
    edtText: TEdit;
    Button3: TButton;
    edtAPIKey: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    edtName: TEdit;
    Label3: TLabel;
    edtRoom: TEdit;
    Memo2: TMemo;
    CheckBox1: TCheckBox;
    GroupBox1: TGroupBox;
    Label4: TLabel;
    edtEmail: TEdit;
    Label5: TLabel;
    edtPassword: TEdit;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
  private
    Lingr: TLingr;
    procedure LingrNotify(Messages, Occupants: TObjectList);
    procedure ExceptionObserve(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.CheckBox1Click(Sender: TObject);
var
  i: Integer;
begin
  for i := 0 to GroupBox1.ControlCount -1 do
    GroupBox1.Controls[i].Enabled := CheckBox1.Checked;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  Room: TLingrChatRoom;
begin
  if Lingr.Session.IsOpen then Exit;
  if edtAPIKey.Text = '' then begin
    ShowMessage('Enter API Key');
    Exit;
  end;
  if edtRoom.Text = '' then begin
    ShowMessage('Enter ChatRoom Name');
    Exit;
  end;
  if edtName.Text = '' then begin
    ShowMessage('Enter NickName');
    Exit;
  end;


  Lingr.APIKey := edtAPIKey.Text;
  Lingr.CreateSession;
  if CheckBox1.Checked then
  
  Lingr.Session.Login(edtEmail.Text, edtPassword.Text);
  Room := Lingr.Session.EnterChatRoom(edtRoom.Text, edtName.Text);
  Room.OnExceptionObserve := ExceptionObserve;
  Room.Observe(LingrNotify);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  Lingr.Session.Close;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  if edtText.Text = '' then Exit;

  if Lingr.Session.ChatRooms.Count = 0 then Exit;
  (Lingr.Session.ChatRooms[0] as TLingrChatRoom).Say(edtText.Text);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Lingr := TLingr.Create;
  Lingr.SetDebugOutput(Memo1.Lines);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  FreeAndNil(Lingr);
end;

procedure TForm1.LingrNotify(Messages, Occupants: TObjectList);
var
  i: Integer;
begin
  for i := 0 to Messages.Count - 1 do begin
    Memo2.Lines.Add((Messages[i] as TLingrMessage).TimeStamp);
    Memo2.Lines.Add((Messages[i] as TLingrMessage).NickName);
    Memo2.Lines.Add((Messages[i] as TLingrMessage).Text);
    Memo2.Lines.Add('');
  end;
end;

procedure TForm1.ExceptionObserve(Sender: TObject);
begin
  ShowMessage((Sender as Exception).Message);
end;

end.
